package cn.meng.base.service;

import cn.hutool.core.date.DateTime;
import cn.meng.base.mapper.SalaryManageMapper;
import cn.meng.base.utils.ExcelUtil;
import cn.meng.base.utils.GenerateUtil;
import cn.meng.base.vo.BusinessException;
import cn.meng.base.vo.SalaryQueryRequest;
import cn.meng.base.vo.SalaryVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author liululu
 * @description 薪酬管理
 * @date 2021-08-20 10:29
 **/
@Service
public class SalaryManageServiceImpl implements SalaryManageService{

    @Autowired
    ExcelUtil excelUtil;

    @Autowired
    SalaryManageMapper mapper;

    /**
     * 新增薪酬信息
     * @param salaryVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CachePut(value = "Salary",key = "#salaryVO")
    public boolean insertSalary(SalaryVO salaryVO) throws BusinessException {
        //设置薪酬主键
        salaryVO.setPkSalary(GenerateUtil.getInstance().generate());
        //设置创建时间
        salaryVO.setCreatedTime(new DateTime());
        //新增薪酬信息
        mapper.insertSalary(salaryVO);
        return true;
    }

    /**
     * 修改薪酬信息
     * @param salaryVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CachePut(value = "Salary",key = "#salaryVO")
    public boolean updateSalary(SalaryVO salaryVO) {
        //获取当前薪酬信息version
        Integer version = mapper.checkVersion(salaryVO.getPkSalary());
        //设置version属性用于加锁校验
        salaryVO.setVersion(version);
        //设置修改时间
        salaryVO.setUpdatedTime(new DateTime());
        //修改薪酬信息
        mapper.updateSalary(salaryVO);
        return true;
    }

    /**
     * 删除薪酬信息
     * @param salaryVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "Salary",key = "#salaryVO")
    public boolean deleteSalary(SalaryVO salaryVO) {
        //获取当前薪酬信息version
        Integer version = mapper.checkVersion(salaryVO.getPkSalary());
        //设置version属性用于加锁校验
        salaryVO.setVersion(version);
        //设置删除时间
        salaryVO.setDeletedTime(new DateTime());
        //逻辑删除薪酬信息
        mapper.deleteSalary(salaryVO);
        return true;
    }

    /**
     * 条件查询薪酬信息
     *
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @Override
    @Cacheable(value = "Salary",key = "#request.payPeriod")
    public PageInfo<SalaryVO> selectByPage(Integer pageNum, Integer pageSize, SalaryQueryRequest request) {

        //设置页码与页容量
        PageHelper.startPage(pageNum,pageSize);

        //按条件查询数据
        List<SalaryVO> list = mapper.selectByCondition(request);

        //将数据封装进PageInfo中
        PageInfo<SalaryVO> info = new PageInfo<>(list);

        return  info;
    }

    /**
     * 导出薪酬信息Excel模板
     *
     * @return
     */
    @Override
    public void exportTemplate(HttpServletResponse response, SalaryVO salaryVO) throws BusinessException {
        excelUtil.exportTemplet(response,salaryVO.getClass());
    }

}
